<?php
namespace application\datatypes;
use application\datatypes\DataType;
class Boolean extends DataType {
	public static $name = 'Boolean';
	public static $fields = array(array('columnType'=>'TINYINT(1)'));
	
	public static function validate($field, $data, $results) {
		$value = $data[$field->cf_column];
		if($value) $value = '1';
		if($field->cf_required && !$value) {
			$results['errors'][] = '<b>'.$field->cf_display_name.'</b> is required.';
		}
		elseif($value) {
			$results['db_fields'][$field->cf_column] = $value;	
		}
		
		return $results;
	}
	
	public static function display($formElements) {
		?>
		
		<fieldset class="single-field">
			<header><?=$formElements['display']?></header>
			<section class="inner">
			<?php if($description = $formElements['description']) : ?>
				<label><?=$formElements['elements']['element']?> <?=$description?></label>
			<?php else:?>
				<label><?=$formElements['elements']['element']?> Edit the field's description.</label>
			<?php endif;?>
			</section>
		</fieldset>
		<?php
	}
	
	public static function build($field, $data) {
		$value = ($data[$field->cf_column]) ? 'checked' : '';
		$formElements = array('description'=>$field->cf_description, 'column'=>$field->cf_column, 'display'=>$field->cf_display_name, 'data_type'=>$field->cf_data_type, 'elements'=>array(
						'element'=>
							sprintf('<input type="checkbox" checked="%1$s" name="%2$s" id="%2$s" value="1"/>', $value, $field->cf_column)
						
		));
		return $formElements;
	
	}
	
	
	
	public static function prepareData($field, $row) {
		if($row[$field->cf_column]) {
			return TRUE;
		}
		else return FALSE;
	}
	
	
}
?>